package com.wx.datastructure.tree.binary;

import com.wx.datastructure.queue.Queue;
import com.wx.datastructure.queue.linked.LinkedQueue;
import com.wx.datastructure.tree.IBinaryTtee;

/**
 * @author wx
 * @Description: 二叉树 小=左边  大=右边
 * @date 2018/12/20 002017:38
 */
public class BinaryTtee<E extends Comparable<E>> implements IBinaryTtee<E> {
    /**
     * @author wx
     * @Description: 节点
     * @date 2018-12-20
     */
    private class Node {
        public E e;
        /**
         * 左节点 右节点
         */
        public Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }


    private Node root;
    private int size;

    public BinaryTtee() {
        root = null;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return this.root == null || this.size == 0;
    }

    @Override
    public void add(E e) {
        this.root = add(this.root, e);
    }

    @Override
    public boolean contains(E e) {
        return contains(this.root, e);
    }

    @Override
    public void preOrder() {
        preOrder(this.root);
    }

    @Override
    public void inOrder() {
        inOrder(this.root);
    }

    @Override
    public void postOrder() {
        postOrder(this.root);
    }

    @Override
    public void levelOrder() {
        Queue<Node> queue = new LinkedQueue<>();
        queue.enqueue(this.root);
        levelOrder(queue);
    }

    @Override
    public E minimum() {
        return this.root == null ? null : getMinNode(this.root).e;
    }

    @Override
    public E maximum() {
        return this.root == null ? null : getMaxNode(this.root).e;
    }

    @Override
    public E removeMin() {
        E value = minimum();
        removeMin(this.root);
        return value;
    }

    @Override
    public E removeMax() {
        E value = maximum();
        removeMax(this.root);
        return value;
    }

    @Override
    public void remove(E e) {
        remove(e,this.root);
    }

    /**
     * @param node
     * @param e
     * @description: 添加元素, 递归方法
     * @author: wx
     * @date: 2018/12/20 0020
     * @return: void
     */
    private Node add(Node node, E e) {
        if (node == null) {
            this.size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;
    }

    /**
     * @param node
     * @param e
     * @description: 验证该元素是否存在, 递归方法
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: boolean
     */
    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) > 0) {
            return contains(node.right, e);
        } else {
            return contains(node.left, e);
        }
    }

    /**
     * @param node
     * @description: 前序便利，根->前序左树->前序右树,递归方法
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * @param node
     * @description: 中序便利-(有序遍历从小到大),递归方法
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);

    }

    /**
     * @param node
     * @description: 后序遍历(后续遍历左树 - > 后续遍历右树 - > 根), 递归方法
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    /**
     * @param queue
     * @description: 层序遍历-从根节点一层一层,递归方法
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private void levelOrder(Queue<Node> queue) {
        if (queue.isEmpty()) {
            return;
        }
        Node node = queue.dequeue();
        System.out.println(node.e);
        if (node.left != null) {
            queue.enqueue(node.left);
        }
        if (node.right != null) {
            queue.enqueue(node.right);
        }
        levelOrder(queue);

    }

    /**
     * @param node
     * @description: 获取最大树
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private Node getMaxNode(Node node) {
        if (node.right == null) {
            return node;
        }
        return getMaxNode(node.right);
    }

    /**
     * @param node
     * @description: 获取最小树
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private Node getMinNode(Node node) {
        if (node.left == null) {
            return node;
        }
        return getMinNode(node.left);
    }

    /**
     * @param node
     * @description: 删除最小节点, 返回删除节点后新的二分搜索树的根
     * @author: wx
     * @date: 2018/12/24 0024
     * @return: com.wx.datastructure.tree.binary.BinaryTtee<E>.Node
     */
    private Node removeMin(Node node) {
        if (node == null) {
            return null;
        }
        if (node.left == null) {
            Node right = node.right;
            node.right = null;
            node.e = null;
            this.size--;
            return right;
        }

        node.left = removeMin(node.left);
        return node;
    }

    /**
     * @param node
     * @description: 删除最大节点, 返回删除节点后新的二分搜索树的根
     * @author: wx
     * @date: 2018/12/24 0024
     * @return: com.wx.datastructure.tree.binary.BinaryTtee<E>.Node
     */
    private Node removeMax(Node node) {
        if (node == null) {
            return null;
        }
        if (node.right == null) {
            Node left = node.left;
            node.left = null;
            node.e = null;
            this.size--;
            return left;
        }

        node.right = removeMax(node.right);
        return node;

    }

    /**
     * @param e
     * @param node
     * @description: 删除节点返回删除节点后新的二分搜索树的根
     * @author: wx
     * @date: 2018/12/24 0024
     * @return: void
     */
    public Node remove(E e, Node node) {
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(e, node.left);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(e, node.right);
            return node;
        } else {
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                node.e = null;
                return rightNode;
            } else if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                node.e = null;
                return leftNode;
            } else {
                // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
                // 用这个节点顶替待删除节点的位置
                Node successor = getMinNode(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.e = null;
                node.left = node.right  = null;
                return successor;
            }
        }
    }

    /**
     * @param node
     * @param depth
     * @param res
     * @description: 生成以node为根节点，深度为depth的描述二叉树的字符串
     * @author: wx
     * @date: 2018/12/22 0022
     * @return: void
     */
    private void generateBSTString(Node node, int depth, StringBuilder res) {

        if (node == null) {
            res.append(generateDepthString(depth) + "null\n");
            return;
        }

        res.append(generateDepthString(depth) + node.e + "\n");
        generateBSTString(node.left, depth + 1, res);
        generateBSTString(node.right, depth + 1, res);
    }

    private String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            res.append("--");
        }
        return res.toString();
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        generateBSTString(root, 0, res);
        return res.toString();
    }
}
